/**
 *
 */

package com.person.modules.person.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.person.common.annotation.SysLog;
import com.person.common.utils.DateUtils;
import com.person.common.utils.PageUtils;
import com.person.common.utils.R;
import com.person.common.validator.ValidatorUtils;
import com.person.modules.person.constant.WorkStatusEnum;
import com.person.modules.person.entity.WorkDailyEntity;
import com.person.modules.person.entity.WorkRecordEntity;
import com.person.modules.person.service.WorkRecordService;
import com.person.modules.sys.controller.AbstractController;


import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 考勤管理
 *
 * @author
 */
@RestController
@RequestMapping("/person/work")
public class WorkRecordController extends AbstractController {
    @Autowired
    private WorkRecordService workRecordService;
    @Value("${work-up-time}")
    String upTimeStr;

    @Value("${work-down-time}")
    String downTimeStr;
    @Value("${work-over-time}")
    String overTimeStr;

    /**
     * 所有考勤列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("person:work:list")
    @CrossOrigin
    public R list(@RequestParam Map<String, Object> params) {
        if(getUserId() != 1){
            //不是管理员只能查看自己工资记录
            params.put("userId", getUserId());
        }
        PageUtils page = workRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 考勤信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("person:work:info")
    @ResponseBody
    @CrossOrigin
    public R info(@PathVariable("id") Long id) {
        WorkRecordEntity work = workRecordService.getById(id);

        return R.ok().put("work", work);
    }

    /**
     * 保存考勤
     */
    @Transactional
    @RequestMapping("/save")
    @RequiresPermissions("person:work:save")
    @CrossOrigin
    public R save(HttpSession session,MultipartFile file) {
    List<WorkRecordEntity> workRecordEntities = new ArrayList<WorkRecordEntity>();
    InputStream in = null;
    Workbook wb = null;
    try {
    	
    	if (file.isEmpty()) {
    		return R.error("文件为空");
		}
    	String filename=file.getOriginalFilename();
	    in = file.getInputStream(); 
	    //得到excel  
	   
	    String fileType = filename.substring(filename.lastIndexOf("."));  
	    if((".xls").equals(fileType))
	    {  
	        wb = new HSSFWorkbook(in);  //2003-  
	    }else if((".xlsx").equals(fileType))
	    {  
	       wb = new XSSFWorkbook(in);  //2007+  
	    }else
	    {  
	    	return R.error("文件格式错误");
	    }   	
    }catch (IOException e)
    {  
	      e.printStackTrace();  
	 }
    workRecordService.update(wb);
    return R.ok();
    }

    /**
     * 修改考勤
     */
	/*
	 * @SysLog("修改考勤")
	 * 
	 * @RequestMapping("/update")
	 * 
	 * @RequiresPermissions("person:work:update")
	 * 
	 * @CrossOrigin public R update(@RequestBody WorkRecordEntity work) {
	 * ValidatorUtils.validateEntity(work);
	 * work.setUpdateTime(DateUtils.currentTimeFormat()); int downTime =
	 * Integer.valueOf(downTimeStr); int overTime = Integer.valueOf(overTimeStr);
	 * Integer down = Integer.valueOf(work.getDownTime().substring(0, 2)); if (down
	 * < downTime) { work.setStatus(WorkStatusEnum.LEAVE.getCode()); } else if (down
	 * >= overTime) { work.setStatus(WorkStatusEnum.OVERTIME.getCode()); }
	 * 
	 * workRecordService.update(work);
	 * 
	 * return R.ok(); }
	 */

    /**
     * 删除考勤
     */
    @SysLog("删除考勤")
    @RequestMapping("/delete")
    @RequiresPermissions("person:work:delete")
    @CrossOrigin
    public R delete(@RequestBody Long[] ids) {
        workRecordService.deleteBatch(ids);
        return R.ok();
    }

}